from fastapi import APIRouter, Depends

from config.Database import get_db
from schemas.AccountUsage import AccountUsageCreate, AccountUsageRequest, AccountUsageUpdate
from schemas.Result import Result
from services.AccountUsageServe import AccountUsageServe
from requests import Session
router = APIRouter(
    prefix="/account",
    tags=["account"],
    responses={404: {"description": "Not found"}},
)

account_usage_serve = AccountUsageServe()

#根据username 添加账号使用次数
@router.post("/setting")
def add_account_usage(info: AccountUsageRequest,  db: Session = Depends(get_db)):
    if info.count <= 0:
        return Result.success(msg="增加的使用次数必须大于0")
    if info.password != "yirc99":
        return Result.fail(msg="增加账号使用次数失败")
    account = account_usage_serve.get_by_username(db,info.username)
    if not account:
        result = account_usage_serve.create(db,AccountUsageCreate(username=info.username,count=info.count))
    else:
        result = account_usage_serve.update(db,account.id,AccountUsageUpdate(count=info.count))

    if result:
        return Result.success(msg=f"成功修改账号 {info.username} 为 {info.count} 次使用次数")
    else:
        return Result.success(msg="修改账号使用次数失败")
